package com.github.ilioili.justdoit.components.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.widget.TextView;

import com.github.ilioili.justdoit.R;
import com.github.ilioili.justdoit.common.AppBaseActivity;
import com.github.ilioili.justdoit.model.dao.RecordDao;
import com.github.ilioili.justdoit.model.dao.ScheduleEventDao;
import com.github.ilioili.justdoit.model.pojo.EventRecord;
import com.github.ilioili.justdoit.model.pojo.ScheduleEvent;
import com.github.ilioili.justdoit.components.receiver.AlarmReceiver;
import com.github.ilioili.justdoit.util.DateUtil;
import com.taihe.template.base.injection.Id;
import com.taihe.template.base.injection.Layout;

import java.util.Calendar;

@Layout(R.layout.activity_notify)
public class NotifyActivity extends AppBaseActivity {

    public static final String INTENT_KEY_HABIT = "ScheduleEvent";
    @Id(R.id.v_delay_time)
    private View vDelayTime;
    @Id(R.id.v_repeat_event)
    private View vRepeatEvent;
    @Id(R.id.v_single_event)
    private View vSingleEvent;
    @Id(R.id.tv_name)
    private TextView tvName;
    @Id(R.id.tv_sub)
    private TextView tvSub;
    private ScheduleEvent scheduleEvent;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        scheduleEvent = (ScheduleEvent) getIntent().getSerializableExtra(INTENT_KEY_HABIT);
        tvName.setText(scheduleEvent.name);
        Calendar calendar = Calendar.getInstance();
        if (scheduleEvent.isRepeat) {
            vSingleEvent.setVisibility(View.GONE);
            tvSub.setText("开始于" + calendar.get(Calendar.YEAR) + "." + (calendar.get(Calendar.MONTH) + 1) + "." + calendar.get(Calendar.DAY_OF_MONTH));
        } else {
            vRepeatEvent.setVisibility(View.GONE);
        }
    }

    public static Intent newIntent(Context context, ScheduleEvent scheduleEvent) {
        Intent intent = new Intent(context, NotifyActivity.class);
        intent.putExtra(INTENT_KEY_HABIT, scheduleEvent);
        return intent;
    }

    public void onClickComplete(View v) {
        scheduleEvent.isComplete = true;
        ScheduleEventDao.replace(scheduleEvent);
        finish();
    }

    public void onClickCompleteRepeat(View v) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        EventRecord eventRecord = new EventRecord(year, month, day, scheduleEvent.id);
        RecordDao.replace(eventRecord);
        finish();
    }

    public void onClickLaterOn(View v) {
        scheduleEvent.alermMinute += 30;
        if(scheduleEvent.alermMinute>59){
            scheduleEvent.alermMinute-=60;
            scheduleEvent.alermHour++;
            if(scheduleEvent.alermHour==24){
                scheduleEvent.alermHour = 0;
            }
        }
        if (scheduleEvent.alermHour < 23) scheduleEvent.alermHour++;
        scheduleEvent.alermMinute += 30;
        if (scheduleEvent.alermMinute > 60) scheduleEvent.alermMinute = 59;
        ScheduleEventDao.replace(scheduleEvent);
        finish();
    }

    public void onClickLaterOnRepeat(View v) {
        vDelayTime.setVisibility(View.VISIBLE);
    }

    public void onClickDelete(View v) {
        Snackbar snackbar = Snackbar.make(v, "删除\"" + scheduleEvent.name + "\"的所有记录吗", Snackbar.LENGTH_LONG);
        snackbar.setAction(R.string.confirm, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ScheduleEventDao.delete(scheduleEvent);
            }
        });
        snackbar.show();
        finish();
    }

    public void onClickDeleteRepeat(View v) {
        Snackbar snackbar = Snackbar.make(v, "删除\"" + scheduleEvent.name + "\"的所有记录吗", Snackbar.LENGTH_LONG);
        snackbar.setAction(R.string.confirm, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ScheduleEventDao.delete(scheduleEvent);
            }
        });
        snackbar.show();
        finish();
    }

    public void onClickTomorrow(View v) {
        scheduleEvent.startTime = DateUtil.getTodayStartTime() + DateUtil.DAY_MILISECOND;
        ScheduleEventDao.replace(scheduleEvent);
        AlarmReceiver.setAlarm(this, scheduleEvent);
        finish();
    }

    public void onClickDayAfterTomorrow(View v) {
        scheduleEvent.startTime = DateUtil.getTodayStartTime() + 2 * DateUtil.DAY_MILISECOND;
        ScheduleEventDao.replace(scheduleEvent);
        AlarmReceiver.setAlarm(this, scheduleEvent);
        finish();
    }

    public void onClickOneDay(View v) {
        scheduleEvent.startTime = Long.MAX_VALUE;
        ScheduleEventDao.replace(scheduleEvent);
        finish();
    }

    public void onClickThisWeek(View v) {
        if (DateUtil.isTheLastDayOfWeek(scheduleEvent.startTime)) {
            Snackbar snackbar = Snackbar.make(v, "今天是本周最后一天，是否推迟到下个星期", Snackbar.LENGTH_LONG);
            snackbar.setAction(R.string.confirm, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    scheduleEvent.startTime = DateUtil.getTodayStartTime() + 3 * DateUtil.DAY_MILISECOND;
                    ScheduleEventDao.replace(scheduleEvent);
                }
            });
        } else {
            scheduleEvent.startTime = DateUtil.getTimeLaterInThisWeek(scheduleEvent.startTime);
        }
        AlarmReceiver.setAlarm(this, scheduleEvent);
        finish();
    }

    public void onClickThisMonth(View v) {
        if (DateUtil.isTheLastDayOfMonth(scheduleEvent.startTime)) {
            Snackbar snackbar = Snackbar.make(v, "今天是本月最后一天，是否推迟到下一个月", Snackbar.LENGTH_LONG);
            snackbar.setAction(R.string.confirm, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    scheduleEvent.startTime = DateUtil.getTodayStartTime() + 7 * DateUtil.DAY_MILISECOND;
                    ScheduleEventDao.replace(scheduleEvent);
                }
            });
        } else {
            scheduleEvent.startTime = DateUtil.getTimeLaterInThisMonth(scheduleEvent.startTime);
            ScheduleEventDao.replace(scheduleEvent);
        }
        AlarmReceiver.setAlarm(this, scheduleEvent);
        finish();
    }

    public void onClick15m(View v) {
        AlarmReceiver.startAlarm(this, scheduleEvent, System.currentTimeMillis() + 15 * 1000 * 60);
        finish();
    }

    public void onClick30m(View v) {
        AlarmReceiver.startAlarm(this, scheduleEvent, System.currentTimeMillis() + 30 * 1000 * 60);
        finish();
    }

    public void onClick1h(View v) {
        AlarmReceiver.startAlarm(this, scheduleEvent, System.currentTimeMillis() + 1 * 60 * 1000 * 60);
        finish();
    }

    public void onClick2h(View v) {
        AlarmReceiver.startAlarm(this, scheduleEvent, System.currentTimeMillis() + 2 * 60 * 1000 * 60);
        finish();
    }

    public void onClick3h(View v) {
        AlarmReceiver.startAlarm(this, scheduleEvent, System.currentTimeMillis() + 3 * 60 * 1000 * 60);
        finish();
    }


    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.drop_to_bottom);
    }
}